package com.qixin.modules.ea.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qixin.modules.ea.entity.EaEntertain;
import com.qixin.modules.ea.entity.EaBookDetail;
import com.qixin.modules.ea.entity.EaApplyDetail;
import com.qixin.modules.ea.entity.EaEntertainReceptionActivity;
import com.qixin.modules.ea.mapper.EaBookDetailMapper;
import com.qixin.modules.ea.mapper.EaApplyDetailMapper;
import com.qixin.modules.ea.mapper.EaEntertainReceptionActivityMapper;
import com.qixin.modules.ea.mapper.EaEntertainMapper;
import com.qixin.modules.ea.service.IEaEntertainService;
import com.qixin.modules.eaApplication.entity.EaBusinessApply;
import com.qixin.modules.eaApplication.entity.EaEntertainApply;
import com.qixin.modules.eaApplication.mapper.EaEntertainApplyMapper;
import com.qixin.modules.rzInvoice.entity.EaBill;
import com.qixin.modules.rzInvoice.mapper.EaBillMapper;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.nc.service.INcInoutbusiclassService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Collection;
import java.util.Map;

/**
 * @Description: ea_entertain
 * @Author: qixin-boot
 * @Date:   2022-01-21
 * @Version: V1.0
 */
@Service
public class EaEntertainServiceImpl extends ServiceImpl<EaEntertainMapper, EaEntertain> implements IEaEntertainService {

	@Autowired
	private EaEntertainMapper eaEntertainMapper;
	@Autowired
	private EaBookDetailMapper eaBookDetailMapper;
	@Autowired
	private EaApplyDetailMapper eaApplyDetailMapper;
	@Autowired
	private EaEntertainReceptionActivityMapper eaEntertainReceptionActivityMapper;
	@Autowired
	private EaEntertainApplyMapper eaEntertainApplyMapper;
	@Autowired
	private EaBillMapper eaBillMapper;
	@Autowired
	private INcInoutbusiclassService ncInoutbusiclassService;

	@Override
	@Transactional
	public void saveMain(EaEntertain eaEntertain, List<EaBookDetail> eaBookDetailList, List<EaEntertainApply> eaEntertainApplyList, List<EaEntertainReceptionActivity> eaEntertainReceptionActivityList) {
		// update-begin-by wujinbiao 2022/2/25 16:08 for 把费用项目名称存到数据库表中
		if(StringUtils.isNotBlank(eaEntertain.getIndexId())){
			String indexIdName = ncInoutbusiclassService.getIndexIdName(eaEntertain.getIndexId());
			eaEntertain.setIndexIdName(indexIdName);
		}
		// update-end-by wujinbiao 2022/2/25 16:08 for 把费用项目名称存到数据库表中
		eaEntertainMapper.insert(eaEntertain);

		// 费用明细
		//update-begin-author wjb 2022-02-10 15:12 for 发票被引用后状态修改
		if(eaBookDetailList!=null && eaBookDetailList.size()>0) {
			for(EaBookDetail entity:eaBookDetailList) {
				//更新发票使用情况
				String fpid = entity.getFpid();
				EaBill eaBill = eaBillMapper.selectById(fpid);
				String isused = eaBill.getIsused();   //发票是否已使用
				if(isused.equals("1")){
					throw new RuntimeException("发票号码:" + eaBill.getNumber() + "已被使用!");
				}else{
					eaBill.setIsused("1");  //设置发票已使用
					eaBillMapper.updateById(eaBill);
				}
				//update-end-author wjb 2022-02-10 15:12 for 发票被引用后状态修改
				//外键设置
				entity.setBillId(eaEntertain.getId());
				eaBookDetailMapper.insert(entity);
			}
		}
		// update-begin-author by wjb 2022-02-09 for 将招待费报销单id填入招待费申请单的字段 ea_travel_id

		// 活动内容
		if(eaEntertainReceptionActivityList != null && eaEntertainReceptionActivityList.size() > 0) {
			for(EaEntertainReceptionActivity entity:eaEntertainReceptionActivityList) {
				//外键设置
				entity.setId(null);
				entity.setPid(eaEntertain.getId());
				eaEntertainReceptionActivityMapper.insert(entity);
			}
		}

		// 关联申请单
		// update-begin-author wjb for 2022-02-10 09:24 for 修改招待费报销单关联申请单逻辑 将报销单的id填入招待费申请单的字段  ea_entertain_id
		if(eaEntertainApplyList != null && eaEntertainApplyList.size() >0){
			EaEntertainApply eaEntertainApply = eaEntertainApplyList.get(0);
			eaEntertainApply.setEaEntertainId(eaEntertain.getId());
			eaEntertainApplyMapper.updateById(eaEntertainApply);
		}

		/*
		if(eaApplyDetailList!=null && eaApplyDetailList.size()>0) {
			for(EaApplyDetail entity:eaApplyDetailList) {
				//外键设置
				entity.setBillId(eaEntertain.getId());
				eaApplyDetailMapper.insert(entity);
			}
		}
		*/
		// update-begin-author wjb for 2022-02-10 09:24 for 修改招待费报销单关联申请单逻辑 将报销单的id填入招待费申请单的字段  ea_entertain_id
	}

	@Override
	@Transactional
	public void updateMain(EaEntertain eaEntertain,List<EaBookDetail> eaBookDetailList,List<EaEntertainApply> eaEntertainApplyList,List<EaEntertainReceptionActivity> eaEntertainReceptionActivityList) {
		// update-begin-by wujinbiao 2022/2/25 16:08 for 把费用项目名称存到数据库表中
		if(StringUtils.isNotBlank(eaEntertain.getIndexId())){
			String indexIdName = ncInoutbusiclassService.getIndexIdName(eaEntertain.getIndexId());
			eaEntertain.setIndexIdName(indexIdName);
		}
		// update-end-by wujinbiao 2022/2/25 16:08 for 把费用项目名称存到数据库表中
		eaEntertainMapper.updateById(eaEntertain);

		//update-begin-author wjb 2022-02-10 15:15 for 修改原发票的关联关系
		//1.修改原发票的关联关系
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("bill_id",eaEntertain.getId());
		List<EaBookDetail> eaBookDetailList1 = eaBookDetailMapper.selectByMap(paramsMap);
		for(EaBookDetail entity:eaBookDetailList1) {
			String fpid = entity.getFpid();  //发票id
			EaBill eaBill = eaBillMapper.selectById(fpid);
			eaBill.setIsused("0");   //设置状态为未使用
			eaBillMapper.updateById(eaBill);
		}
		//update-end-author wjb 2022-02-10 15:15 for 修改原发票的关联关系


		//2.先删除子表数据
		eaBookDetailMapper.deleteByMainId(eaEntertain.getId());
		eaEntertainReceptionActivityMapper.deleteByMainId(eaEntertain.getId());
		
		//3.子表数据重新插入
		if(eaBookDetailList!=null && eaBookDetailList.size()>0) {
			for(EaBookDetail entity:eaBookDetailList) {
				//更新发票使用情况
				String fpid = entity.getFpid();
				EaBill eaBill = eaBillMapper.selectById(fpid);
				String isused = eaBill.getIsused();   //发票是否已使用
				if(isused.equals("1")){
					throw new RuntimeException("发票号码:" + eaBill.getNumber() + "已被使用!");
				}else{
					eaBill.setIsused("1");  //设置发票已使用
					eaBillMapper.updateById(eaBill);
				}
				//外键设置
				entity.setBillId(eaEntertain.getId());
				eaBookDetailMapper.insert(entity);
			}
		}

//		if(eaApplyDetailList!=null && eaApplyDetailList.size()>0) {
//			for(EaApplyDetail entity:eaApplyDetailList) {
//				//外键设置
//				entity.setBillId(eaEntertain.getId());
//				eaApplyDetailMapper.insert(entity);
//			}
//		}

		if(eaEntertainReceptionActivityList!=null && eaEntertainReceptionActivityList.size()>0) {
			for(EaEntertainReceptionActivity entity:eaEntertainReceptionActivityList) {
				//外键设置
				entity.setPid(eaEntertain.getId());
				eaEntertainReceptionActivityMapper.insert(entity);
			}
		}

		//update-end-author wjb 2022-02-10 15:23 for 关联申请单修改
		//1、原关联的申请单取消关联
		QueryWrapper<EaEntertainApply> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("ea_entertain_id",eaEntertain.getId());
		EaEntertainApply eaEntertainApply = eaEntertainApplyMapper.selectOne(queryWrapper);
		if(eaEntertainApply != null){
			eaEntertainApply.setEaEntertainId(null);
			eaEntertainApplyMapper.updateByBillId(eaEntertainApply.getId());
		}
		//2、关联新的申请单
		if(eaEntertainApplyList != null && eaEntertainApplyList.size() > 0){
			EaEntertainApply newEaEntertainApply = eaEntertainApplyList.get(0);
			newEaEntertainApply.setEaEntertainId(eaEntertain.getId());
			eaEntertainApplyMapper.updateById(newEaEntertainApply);
		}
		//update-end-author wjb for 关联申请单修改
	}

	@Override
	@Transactional
	public void delMain(String id) {
		//update-begin-author wjb 2022-02-10 15:40 删除申请单、发票关联关系
		//1、取消招待费申请单关联信息 ea_entertain_id 字段置为空
		QueryWrapper<EaEntertainApply> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("ea_entertain_id",id);
		EaEntertainApply eaEntertainApply = eaEntertainApplyMapper.selectOne(queryWrapper);
		if(eaEntertainApply != null){
			eaEntertainApplyMapper.updateByBillId(eaEntertainApply.getId());
		}

		//2、费用明细表中的发票改为 未使用
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("bill_id",id);
		List<EaBookDetail> eaBookDetailList1 = eaBookDetailMapper.selectByMap(paramsMap);
		for(EaBookDetail entity:eaBookDetailList1) {
			String fpid = entity.getFpid();  //发票id
			EaBill eaBill = eaBillMapper.selectById(fpid);
			if (eaBill != null){
				eaBill.setIsused("0");   //设置状态为未使用
				eaBillMapper.updateById(eaBill);
			}
		}

		//update-end-author wjb 2022-02-10 15:40 删除申请单、发票关联关系

		eaBookDetailMapper.deleteByMainId(id);
		eaEntertainReceptionActivityMapper.deleteByMainId(id);
		eaEntertainMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			eaBookDetailMapper.deleteByMainId(id.toString());
			eaApplyDetailMapper.deleteByMainId(id.toString());
			eaEntertainReceptionActivityMapper.deleteByMainId(id.toString());
			eaEntertainMapper.deleteById(id);
		}
	}
	
}
